import { UILayer } from "./UILayer";
import { TweenLite } from "../../../com/greensock/TweenLite";
import { Global } from "../../../com/gengine/global/Global";
import { IView } from "../../mvc/interfaces/IView";
import { Cubic } from "../../../com/greensock/easing/Cubic";
import { Window } from "../../component/window/Window";
//class WindowLayer
    
    export  class WindowLayer extends UILayer
    {
        constructor()
        {
            super();
            this.dcPopingWindow = new Map<any, any> /* flash.utils.Dictionary */(true);
            return;
        }

        public /* override */ addPopUp(arg1: Object /* flash.display.DisplayObject */, arg2: Boolean=false): void
        {
            var displayObject: Object /* flash.display.DisplayObject */;
            var modal: Boolean=false;
            var tl: TweenLite;

            var loc1;
            tl = null;
            displayObject = arg1;
            modal = arg2;
            if (displayObject && (this.contains(displayObject) == false || this.isPoping(displayObject))) 
            {
                if (displayObject.localToGlobal(new Object /* flash.geom.Point */()).x + 100 > Global.stage.stageWidth) 
                {
                    this.centerPopup(displayObject);
                }
                this.addChild(displayObject);
                if (typeof displayObject === "iview") 
                {
                    displayObject.alpha = 0;
                    tl = new TweenLite(displayObject, 0.5, {"alpha": 1, "ease": Cubic.easeOut, "onComplete":(): void
                    {
                        removePoping(displayObject);
                        return;
                    }})
                    this.addPoping(displayObject, tl);
                }
            }
            return;
        }

        public /* override */ removePopup(arg1: Object /* flash.display.DisplayObject */, arg2: Boolean=true): void
        {
            var displayObject: Object /* flash.display.DisplayObject */;
            var tweenable: Boolean=true;
            var tl: TweenLite;

            var loc1;
            tl = null;
            displayObject = arg1;
            tweenable = arg2;
            if (this.contains(displayObject)) 
            {
                if (tweenable) 
                {
                    displayObject.alpha = 1;
                    this.setObjMouseEnable(displayObject, false);
                    tl = new TweenLite(displayObject, 0.5, {"alpha": 0, "ease": Cubic.easeOut, "onComplete":(): void
                    {
                        removePoping(displayObject);
                        removeChild(displayObject);
                        setObjMouseEnable(displayObject, true);
                        return;
                    }})
                    this.addPoping(displayObject, tl);
                }
                else 
                {
                    this.removePoping(displayObject);
                    removeChild(displayObject);
                    this.setObjMouseEnable(displayObject, true);
                }
            }
            return;
        }

        /* internal  */addPoping(arg1: Object, arg2: TweenLite): void
        {
            this.removePoping(arg1);
            this.dcPopingWindow[arg1] = arg2;
            arg2.play();
            return;
        }

        /* internal  */isPoping(arg1: Object): Boolean
        {
            var loc1=this.dcPopingWindow[arg1];
            return !(loc1 == null);
        }

        /* internal  */removePoping(arg1: Object): void
        {
            var loc1=null;
            if (this.isPoping(arg1)) 
            {
                loc1 = this.dcPopingWindow[arg1];
                loc1.pause();
                loc1.kill();
                loc1 = null;
                this.setObjMouseEnable(arg1, true);
                delete this.dcPopingWindow[arg1];
            }
            return;
        }

        /* internal  */setObjMouseEnable(arg1: Object, arg2: Boolean): void
        {
            if (arg1 is flash.display.InteractiveObject) 
            {
                (arg1 as Object /* flash.display.InteractiveObject */).mouseEnabled = arg2;
            }
            if (arg1 is flash.display.DisplayObjectContainer) 
            {
                (arg1 as Object /* flash.display.DisplayObjectContainer */).mouseChildren = arg2;
            }
            return;
        }

        public /* override */ centerPopup(arg1: Object /* flash.display.DisplayObject */): void
        {
            arg1.x = (Global.stage.stageWidth - arg1.width) / 2;
            arg1.y = (Global.stage.stageHeight - arg1.height) / 2;
            if (Global.stage.stageHeight - arg1.height - 100 < arg1.y) 
            {
                arg1.y = Global.stage.stageHeight - arg1.height - 100;
            }
            if (Global.stage.stageWidth - arg1.width - arg1.x - 280 < 0) 
            {
                arg1.x = Global.stage.stageWidth - arg1.width - 280;
            }
            if (arg1.x < 0) 
            {
                arg1.x = 0;
            }
            if (arg1.y < 0) 
            {
                arg1.y = 0;
            }
            return;
        }

        public leftToChat(arg1: Object /* flash.display.DisplayObject */): void
        {
            if (!this.contains(arg1) || !(arg1 is IView)) 
            {
                return;
            }
            arg1.x = 280;
            return;
        }

        public rightToTask(arg1: Object /* flash.display.DisplayObject */): void
        {
            if (!this.contains(arg1)) 
            {
                return;
            }
            arg1.x = Global.stage.stageWidth - arg1.width - 280;
            return;
        }

        public centerWindows(... rest): void
        {
            var loc1=0;
            var loc2=new Vector.<Window>();
            var loc3=10000;
            var loc4=5;
            var loc5=0;
            while (loc5 < rest.length) 
            {
                if (typeof rest[loc5] === "window") 
                {
                    loc2.push(rest[loc5] as Window);
                    loc1 = loc1 + (rest[loc5] as Window).width;
                    this.centerPopup(rest[loc5] as Window);
                    loc3 = Math.min(loc3, (rest[loc5] as Window).y);
                }
                else if (typeof rest[loc5] === "number") 
                {
                    loc4 = rest[loc5];
                }
                ++loc5;
            }
            loc1 = loc1 + (loc2.length - 1) * loc4;
            var loc6=(Global.stage.stageWidth - loc1) / 2;
            var loc7=0;
            while (loc7 < loc2.length) 
            {
                if (loc2[loc7].isHide) 
                {
                    loc2[loc7].show();
                }
                this.setTop(loc2[loc7]);
                loc2[loc7].x = loc6;
                loc2[loc7].y = loc3;
                loc6 = loc6 + (loc2[loc7].width + loc4);
                ++loc7;
            }
            return;
        }

        public tweenCenterWindow(... rest): void
        {
            var loc8=NaN;
            var loc1=0;
            var loc2=new Vector.<Window>();
            var loc3=10000;
            var loc4=5;
            var loc5=0;
            while (loc5 < rest.length) 
            {
                if (typeof rest[loc5] === "window") 
                {
                    loc2.push(rest[loc5] as Window);
                    loc1 = loc1 + (rest[loc5] as Window).width;
                    this.centerPopup(rest[loc5] as Window);
                    loc3 = Math.min(loc3, (rest[loc5] as Window).y);
                }
                else if (typeof rest[loc5] === "number") 
                {
                    loc4 = rest[loc5];
                }
                ++loc5;
            }
            loc1 = loc1 + (loc2.length - 1) * loc4;
            var loc6=(Global.stage.stageWidth - loc1) / 2;
            var loc7=0;
            while (loc7 < loc2.length) 
            {
                if (loc2[loc7].isHide) 
                {
                    loc2[loc7].show();
                }
                this.setTop(loc2[loc7]);
                loc8 = (Global.stage.stageWidth - loc2[loc7].width) / 2 >= loc6 ? loc6 + loc2[loc7].width / 3 : loc6 - loc2[loc7].width / 3;
                this.tweenWindow(loc2[loc7], loc8, loc3, loc6, loc3, 0.3);
                loc6 = loc6 + (loc2[loc7].width + loc4);
                ++loc7;
            }
            return;
        }

        public tweenWindow(arg1: Window, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number=0.7): void
        {
            arg1.x = arg2;
            arg1.y = arg3;
            TweenLite.to(arg1, arg6, {"x": arg4, "y": arg5});
            return;
        }

        /* internal */ /* var */dcPopingWindow: Map<any, any> /* flash.utils.Dictionary */;
    }
